package com.ymttest.database.mongo;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONObject;

import com.mongodb.WriteResult;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.database.MongoDBDriver;
import com.ymttest.utils.EnvSetup;

/**
 * MQ_Cfg 默认selectCollection MQ_App_Cfg
 * 
 * @author zhouyi
 *
 */
public class MQCfgMongoWapper {
	MongoDBDriver mdd;

	public MQCfgMongoWapper() throws Exception {
//		mdd = new MongoDBDriver(EnvSetup.getSystemUrl("mqconfig_mongoip"), Integer.valueOf(EnvSetup
//				.getSystemUrl("mqconfig_mongoport")), "MQ_Configuration_201505");
//		mdd.selectCollection("MQ_App_Cfg");
		mdd = new MongoDBDriver(EnvSetup.getSystemUrl("mqconfig_mongoip"), Integer.valueOf(EnvSetup
				.getSystemUrl("mqconfig_mongoport")), "MQ_Configuration_201505");
		mdd.selectCollection("MQ_App_Cfg");
	}

	/**
	 * 改变表
	 * 
	 * @param Collection
	 *            表名
	 */
	public void changeCollection(String Collection) {
		mdd.selectCollection(Collection);
	}

	/**
	 * 获取一条记录
	 * 
	 * @param id
	 *            _id
	 * @return 结果对象 JSONObject
	 * @throws Exception
	 */
	public JSONObject getOneCfgInfoById(String id) throws Exception {
		String re = null;
		JSONObject json = null;
		ShopCartMongoBean scmb = null;
		ArrayList<String> list = mdd.selectPartLimit("{\"_id\":\"" + id + "\"}", 1);
		try {
			re = list.get(0);
			json = FormatUtil.JSONToJSONObject(re);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return json;
	}

	/**
	 * 根据id pushAll
	 * 
	 * @param id
	 *            _id
	 * @param pushAllJson
	 *            目前暂时请使用getMessageCfgList getMQAppdomainCfgList参数获取list
	 * @return WriteResult
	 * @throws Exception
	 */
	public WriteResult pushAllCfgInfoById(String id, String pushAllJson) throws Exception {
		String re = null;
		JSONObject json = null;
		ShopCartMongoBean scmb = null;
		WriteResult wr = mdd.update("{\"_id\":\"" + id + "\"}", "{$pushAll:" + pushAllJson + "}");
		return wr;
	}

	/**
	 * 根据id set 值
	 * 
	 * @param id
	 * @param seJson
	 *            例如 {"Version":100}
	 * @return
	 * @throws Exception
	 */
	public WriteResult setCfgInfoById(String id, String seJson) throws Exception {
		String re = null;
		JSONObject json = null;
		ShopCartMongoBean scmb = null;
		WriteResult wr = mdd.update("{\"_id\":\"" + id + "\"}", "{$set:" + seJson + "}");
		return wr;
	}

	/**
	 * 获取MessageCfgList
	 * 
	 * @param Q
	 * @param Code
	 * @param url
	 * @return MessageCfgList.toString
	 */
	public String getMessageCfgList(String Q, String Code, String url) {
		List<String> lists = new ArrayList<String>();
		MessageCfgBean msc = new MessageCfgBean();
		msc.setCode(Code);
		msc.setEnable(true);
		ConsumeCfg c = new ConsumeCfg();
		c.setIsAutoAcknowledge(false);
		c.setMaxThreadCount(256);
		c.setPrefetchCount(10);
		c.setRetryTimeOut(10);
		msc.setConsumeCfg(c);
		ExchangeCfg exchangeCfg = new ExchangeCfg();
		exchangeCfg.setExchangeName(Code);
		msc.setExchangeCfg(exchangeCfg);
		QueueCfg queueCfg = new QueueCfg();
		queueCfg.setQueueName(Code);
		msc.setQueueCfg(queueCfg);
		List<CallbackCfg> callbackCfgList = new ArrayList<CallbackCfg>();
		CallbackCfg callbackCfg = new CallbackCfg();
		callbackCfg.setCallbackKey(Q + "_" + Code + "_1");
		callbackCfg.setIsRetry(1);
		callbackCfg.setUrl(url);
		callbackCfgList.add(callbackCfg);
		msc.setCallbackCfgList(callbackCfgList);
		String msgc = FormatUtil.beanToGSONString(msc);
		lists.add(msgc);
		return lists.toString();

	}

	/**
	 * 获取MQAppdomainCfgList
	 * 
	 * @param appid
	 * @param Code
	 * @return MQAppdomainCfgList.toString
	 */
	public String getMQAppdomainCfgList(String appid, String Code) {
		List<String> lists = new ArrayList<String>();
		MQAppdomainCfgItemBean msc = new MQAppdomainCfgItemBean();
		msc.setCode(Code);
		msc.setAppId(appid);
		msc.set_Status(0);
		msc.setConnectionPoolSize(3);
		lists.add(FormatUtil.beanToGSONString(msc));
		return lists.toString();

	}

	/**
	 * 判断用户是否进入黑名单，如果进入则需要释放
	 * @param loginId
	 * @return
	 */
//	public boolean releaseUserFromBlackList(String loginId){
//		
//	}
	
	public static void main(String args[]) {
		try {
			MQCfgMongoWapper mqc = new MQCfgMongoWapper();
			String Q = "test2";
			String Code = "test_20150730003";
			String url = "http://192.168.1.247:773/api/Home/";
			String MessageCfgList = mqc.getMessageCfgList(Q, Code, url);
			String pushAll = "{\"MessageCfgList\":" + MessageCfgList + "}";
			mqc.pushAllCfgInfoById(Q, pushAll);
			String seJson = "{\"Version\":" + (mqc.getOneCfgInfoById("test2").getInt("Version") + 1) + "}";
			mqc.setCfgInfoById(Q, seJson);
			Logger.debug(mqc.getOneCfgInfoById("test2").toString());
			mqc.changeCollection("MQ_Appdomain_Cfg");
			String items = mqc.getMQAppdomainCfgList(Q, Code);
			String pushAll2 = "{\"Items\":" + items + "}";
			mqc.pushAllCfgInfoById("ad_test2", pushAll2);
			Logger.debug(mqc.getOneCfgInfoById("ad_test2").toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	protected void close() throws Throwable {
		mdd.close();
	}
}
